unit AddResultsGui;

interface

uses
  GlobalRecordsAndEnums, Items, Results, InventionCalc, AddConfigurationGui,

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TfrmAddResult = class(TForm)
    gbConfiguration: TGroupBox;
    lstConfigurations: TListView;
    gbResults: TGroupBox;
    btnAdd: TButton;
    btnCancel: TButton;
    lblItem: TLabel;
    lblConfiguration: TLabel;
    lblSuccesses: TLabel;
    lblAttempts: TLabel;
    edtResultAttempts: TEdit;
    edtResultSuccesses: TEdit;
    lblConfigurationDoubleClick: TLabel;
    btnNewConfiguration: TButton;
    lblResultItem: TLabel;
    lblResultConfiguration: TLabel;
    btnHelp: TButton;
    procedure lstConfigurationsChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure btnAddClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnNewConfigurationClick(Sender: TObject);
    procedure lstConfigurationsDblClick(Sender: TObject);
    procedure btnHelpClick(Sender: TObject);
  private
    _item: TNewItem;
    inventionCalc: TInventionCalc;

    procedure LoadConfigurations;
  public
    constructor Create(AOwner: TComponent; invCalc: TInventionCalc; newitem: TNewItem); reintroduce;

    property Item: TNewItem read _item; // determines for which item a result is being added. Used when this form has lost focus and the user ignores that (in which case this form is somply shown again). Then this form shows the wrong item.
  end;
var
  frmAddResult: TfrmAddResult;

implementation

{$R *.dfm}

uses
  ExtActns;

constructor TfrmAddResult.Create(AOwner: TComponent; invCalc: TInventionCalc; newitem: TNewItem);
begin
  inherited Create(AOwner);
  inventionCalc := invCalc;

  LoadConfigurations;

  _item := newitem;
  lblResultItem.Caption := _item.Name;
end;

procedure TfrmAddResult.LoadConfigurations;
var
  i: Integer;
  item: TListitem;
  config: TConfiguration;
begin
  lstConfigurations.Clear;
  for i := 0 to inventionCalc.Results.ConfigurationCount -1 do begin
    config := inventionCalc.Results.GetConfigurationByIndex(i);
    item := lstConfigurations.Items.Add;
    item.Caption := config.Name;
    item.Data := config;
  end;
  lstConfigurations.AlphaSort;
end;

procedure TfrmAddResult.lstConfigurationsChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
  if (lstConfigurations.Selected <> nil) then begin
    lblResultConfiguration.Caption := TConfiguration(lstConfigurations.Selected.Data).Name;
    lstConfigurations.Color := clWindow;
  end;
end;

procedure TfrmAddResult.btnAddClick(Sender: TObject);
var
  i: Integer;
  canclose: Boolean;
begin
  canclose := true;
  lstConfigurations.Color := clWindow;
  edtResultAttempts.Color := clWindow;
  edtResultSuccesses.Color := clWindow;
  if (lstConfigurations.Selected = nil) then begin
    lstConfigurations.Color := clRed;
    canclose := false;
  end;
  if (not TryStrToInt(edtResultAttempts.Text, i)) then begin
    edtResultAttempts.Color := clRed;
    canclose := false;
  end;
  if (not TryStrToInt(edtResultSuccesses.Text, i)) then begin
    edtResultSuccesses.Color := clRed;
    canclose := false;
  end;
  if (canclose) then begin
    for i := 0 to StrToInt(edtResultAttempts.Text) -1 do begin
      inventionCalc.Results.AddResult(_item, TConfiguration(lstConfigurations.Selected.Data), (i < StrToInt(edtResultSuccesses.Text)));
    end;
    self.Close;
    self.Release;
  end;
end;

procedure TfrmAddResult.btnCancelClick(Sender: TObject);
begin
  self.Close;
  self.Release;
end;

procedure TfrmAddResult.btnHelpClick(Sender: TObject);
var
  run: TFileRun;
begin
  run := TFileRun.Create(self);
  run.FileName := 'help\AddResults.html';
  run.Execute;
  run.Free;
end;

procedure TfrmAddResult.btnNewConfigurationClick(Sender: TObject);
var
  gui: TfrmAddConfiguration;
begin
  gui := TfrmAddConfiguration.Create(self, inventionCalc);
  gui.ShowModal;
  gui.Destroy;
  LoadConfigurations;

end;

procedure TfrmAddResult.lstConfigurationsDblClick(Sender: TObject);
var
  config: TConfiguration;
  s: String;
begin
  if (lstConfigurations.Selected <> nil) then begin
    config := TConfiguration(lstConfigurations.Selected.Data);
    s := 'Details for configuration ' + config.Name + ':' + #10#13#10#13;
    if (config.DecryptorType = nil) then s := s + 'Decryptor type: No decryptor' + #10#13
    else s := s + 'Decryptor type: ' + config.DecryptorType.Name + #10#13;
    s := s + 'Meta-item level: ' + IntToStr(config.MetaLevel) + #10#13;
    s := s + 'BPC is max runs: ' + BoolToStr(config.BPCMaxRuns, true) + #10#13;
    s := s + 'BPC runs: ' + IntToStr(config.BPCRuns) + #10#13;
    s := s + 'BPC ME: ' + IntToStr(config.BPCME) + #10#13;
    s := s + 'BPC PE: ' + IntToStr(config.BPCPE) + #10#13;
    s := s + 'Location: ' + config.Location.Name + #10#13;
    MessageDlg(s, mtInformation, [mbOk], 0);
  end;
end;

end.
